//
//  SelDeliveryDishListViewController.m
//  OrderSeat
//
//  Created by 王威 on 13-12-6.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "SelDeliveryDishListViewController.h"

#define TABLE_CELL_HEIGHT 54

@interface SelDeliveryDishListViewController ()
{
    int totalCount;
    float totalMoney;
}

@end

@implementation SelDeliveryDishListViewController


- (id)init
{
    self = [super initWithNibClass:[SelDeliveryDishListViewController class]];
    if (self) {
        _dataSource = [[SelDeliveryDishListDataSource alloc] initWithVC:self];
        _selectedDishList = [[NSMutableArray alloc] initWithCapacity:10];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI
{
    [self setNavigationTitle:@"已点菜单"];
    [self createLeftButton];
    
    self.selectedDishTable.delegate = self.dataSource;
    self.selectedDishTable.dataSource = self.dataSource;
    [self.selectedDishTable reloadData];
    
    [self.selectedDishTable autoHeight];
    [self.selectedDishTable drawBorder];
    
    self.totalView.top = self.selectedDishTable.bottom_5;
    self.btnSubmit.top = self.totalView.bottom_20;
    [self.btnSubmit setTitleForNormal:[NSString stringWithFormat:@"拨打电话 %@",self.tel]];
    [self.totalView drawBorder];
    
    [self.theScrollview autoHeight];
    
    [self totalAction];
}

- (IBAction)leftButtonClick:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)selectDishCountFinished
{
    [self totalAction];
}

- (void)totalAction
{
    totalCount = 0;
    totalMoney = 0;
    
    for (NSDictionary *dic in _selectedDishList) {
        int count = [[dic objectForKey:@"fenshu"] intValue];
        float money = [[dic objectForKey:@"price"] floatValue];
        
        totalCount += count;
        totalMoney += money*count;
    }
    
    self.totalCountLabel.text = [NSString stringWithFormat:@"/%d道菜",totalCount];
    self.totalMoneyLabel.text = [NSString stringWithFormat:@"￥%.0f元",totalMoney];
}

- (IBAction)doneOrderAction:(id)sender
{
    NSString *tel = [NSString stringWithFormat:@"tel:%@", self.tel];
    
    UIWebView *callWebview =[[UIWebView alloc] init];
    NSURL *telURL =[NSURL URLWithString:tel];
    [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
    
    [self.view addSubview:callWebview];
}

@end

//===========================================================================================
#pragma ===================数据源、数据请求等封装================================================
//===========================================================================================

@implementation SelDeliveryDishListDataSource

- (id)initWithVC:(id)viewController
{
    self = [super init];
    if(self) {
        self.viewController = viewController;
    }
    
    return self;
}

#pragma ===================加载tableview数据 Begin================================================

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.viewController.selectedDishList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"SelectedDishCell";
    
    SelectedDishCell *cell = [_viewController createCellWithNib:CellIdentifier tableview:tableView];
    
    NSMutableDictionary *dic = [_viewController.selectedDishList objectAtIndex:indexPath.row];
    [cell setContent:dic];
    cell.delegate = self.viewController;
    
    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

#pragma ===================加载tableview数据 End================================================

@end
